﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using org.se.fs.presenter.IView.Template;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.View.Profile
{
    public class ClientLists : Template.ViewLists
    {
        public ClientLists(IFormLists view) : this(view, new Task.Context.Client()) { }

        public ClientLists(IFormLists view, Crud task)
        {
            View = view;
            Task = task;
        }

        protected override void InitializeSearchOption()
        {
            SearchOption = new List<SearchOption>
                                {
                                    new SearchOption("Lastname", "last_name", typeof (string)),
                                    new SearchOption("Firstname", "first_name", typeof (string)),
                                    new SearchOption("Gender", "gender", typeof (string)),
                                    new SearchOption("Address", "address", typeof (string)),
                                    new SearchOption("Who Create", "user_create_id", typeof (UInt32))
                                };
            var type = (SearchOption.Select(a => a.Option)).ToArray();
            View.FillType(type);
            View.IndexType = -1;
        }

        public override void DisplayData()
        {
            View.StatusLists = string.Format("Result(s): {0} found", Lists.Rows.Count);

            if (Lists.Rows.Count.Equals(0))
                View.Lists = new DataTable();

            SetStatusPage();

            var a =
                (Lists.AsEnumerable().OrderBy(b => b.Field<string>("middle_name")).OrderBy(
                    b => b.Field<string>("first_name")).OrderBy(b => b.Field<string>("last_name")));
            Lists = a.CopyToDataTable();
            var d = (Lists.AsEnumerable().Select(c => new
            {
                Name = string.Format("{0}, {1} {2}.", c.Field<string>("last_name"), c.Field<string>("first_name"), c.Field<string>("middle_name").Substring(0, 1)),
                Gender = c.Field<string>("gender"),
                Address = c.Field<string>("address"),
                Age = Math.Floor(DateTime.Now.Subtract(c.Field<DateTime>("birth_date")).TotalDays / 365),
                UserName = c.Field<string>("user_name")
            })).ToArray();
            View.Lists = d;
        }
    }
}
